import { Provide, Query, Body } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Photo } from '../entity/photo.entity';
import { Repository } from 'typeorm';
import { PhotoDataError } from '../error/photo.error';

@Provide()
export class PhotoService {
  @InjectEntityModel(Photo)
  photoModel: Repository<Photo>;

  // find
  async findPhotos() {
    try {
      // find All
      const allPhotos = await this.photoModel.find({});
      console.log('All photos from the db: ', allPhotos);

      // find first
      const firstPhoto = await this.photoModel.findOne({
        where: {
          id: 1,
        },
      });
      console.log('First photo from the db: ', firstPhoto);

      // find one by name
      const meAndBearsPhoto = await this.photoModel.findOne({
        where: { name: 'Me and Bears' },
      });
      console.log('Me and Bears photo from the db: ', meAndBearsPhoto);

      // find by views
      const allViewedPhotos = await this.photoModel.find({
        where: { views: 1 },
      });
      console.log('All viewed photos: ', allViewedPhotos);

      const allPublishedPhotos = await this.photoModel.find({
        where: { isPublished: true },
      });
      console.log('All published photos: ', allPublishedPhotos);

      // find and get count
      const [allPhotos2, photosCount] = await this.photoModel.findAndCount({});
      console.log('All photos: ', allPhotos2);
      console.log('Photos count: ', photosCount);

      return {
        allPhotos,
        firstPhoto,
        meAndBearsPhoto,
        allViewedPhotos,
        allPublishedPhotos,
        allPhotos2,
        photosCount,
      };
    } catch (error) {
      throw new PhotoDataError(error);
    }
  }

  async findPhotoById(@Query('id') id: number) {
    try {
      const photo = await this.photoModel.findOne({
        where: {
          id,
        },
      });

      return {
        photo,
      };
    } catch (error) {
      throw new PhotoDataError(error);
    }
  }

  // save
  async savePhoto(@Body() photo: Photo) {
    try {
      // save entity
      const photoResult = await this.photoModel.save(photo);

      // save success
      console.log('photo id = ', photoResult);

      return {
        id: photoResult.id,
      };
    } catch (error) {
      throw new PhotoDataError(error);
    }
  }

  // update
  async updatePhoto(@Body() photo: Photo) {
    try {
      const photoToUpdate = await this.photoModel.findOne({
        where: {
          id: photo.id,
        },
      });
      photoToUpdate.name = photo.name;

      const photoResult = await this.photoModel.save(photoToUpdate);

      return {
        id: photoResult.id,
      };
    } catch (error) {
      throw new PhotoDataError(error);
    }
  }

  // delete
  async deletePhotoById(@Query('id') id: number) {
    try {
      const photo = await this.photoModel.findOne({
        where: {
          id,
        },
      });

      // 删除单个
      const photoResult = await this.photoModel.remove(photo);

      return {
        id: photoResult.id,
      };
    } catch (error) {
      throw new PhotoDataError(error);
    }
  }
}
